package com.stone.business.jc.action;

/**
 * <pre>
 * 月计划
 * @author
 * </pre>
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.jc.pojo.TKs;
import com.stone.business.jc.service.JcCommonService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseListAction;

public class PlanMonthListAction extends BaseListAction {

	private static final long serialVersionUID = 3979577965804736350L;

	private JcCommonService myService;
	
	public JcCommonService getMyService() {
		return myService;
	}

	public void setMyService(JcCommonService myService) {
		this.service = myService;
		this.myService = myService;
	}
	
    /** * 构造方法. */
    public PlanMonthListAction() {
        this.entityClass = TKs.class;
    }
    
    String bz;
    
    String isAdmin;
    
    String month;
    
    String currentmonth;
    
    String monthid;
	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getCurrentmonth() {
		return currentmonth;
	}

	public void setCurrentmonth(String currentmonth) {
		this.currentmonth = currentmonth;
	}

	public String getMonthid() {
		return monthid;
	}

	public void setMonthid(String monthid) {
		this.monthid = monthid;
	}

	/**
	 * 设置查询条件
	 */
	public HashMap<String, Object> setSearchCondition() {
		
		HashMap<String, Object> m = new HashMap<String, Object>();
		
		List<Object> valsList = new ArrayList<Object>();
		
		String sql = "SELECT TO_CHAR(PM.PLANDATE,'YYYY-MM') AS PLANMONTH FROM TAB_PLAN_MONTH PM GROUP BY TO_CHAR(PM.PLANDATE,'YYYY-MM') ORDER BY PLANMONTH DESC";
		m.put("vals", valsList.toArray());
		m.put("pageflag", "sql");
		m.put("sql", sql);
		
		
		TUser user = (TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER);
		
		// 安全员
		if ("006".equals(user.getJxkhgw())) {
			
			this.bz = "安全员";
			
			// 技术员
		} else if ("004".equals(user.getJxkhgw())) {
			
			this.bz = "技术员";
			
			// 操作队队长
		} else if ("005".equals(user.getJxkhgw())) {
			
			this.bz = ((TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganName();
			
			// 维修班长
		} else if ("007".equals(user.getJxkhgw())) {
			
			this.bz = "维修班长";
			
		} else {
			this.bz = "";
		}
		
		isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		
		return m;
		
	}
public void getMonthDate() throws Exception {
		
		List<HashMap<String,Object>> listTree;
		
		// 左侧的树
		if ("-1".equals(month)) {
			
			listTree = myService.findListBySql("select to_char(plandate,'YYYY') as text, to_char(plandate,'YYYY') as id, 0 as leaf from tab_plan_month group by to_char(plandate,'YYYY') order by id");

		// 维护时根节点
		} else if (month.length() == 4) {
			listTree = myService.findListBySql("select to_char(plandate,'MM') as text, '" + month + "' || '-' || to_char(plandate,'MM') as id, 1 as leaf  from tab_plan_month where to_char(plandate,'YYYY') = '" + month + "' group by to_char(plandate,'MM') order by id");
		} else {
			listTree = myService.findListBySql("select to_char(plandate,'MM') as text, '" + month + "' || '-' || to_char(plandate,'MM') as id, 1 as leaf  from tab_plan_month where to_char(plandate,'YYYY') = '" + month + "' group by to_char(plandate,'MM') order by id");
			//listTree = myService.findListBySql("select to_char(planweekstart,'DD') || '~' || to_char(planweekend,'DD') as text, id as id, 1 as leaf from tab_plan_week where to_char(planweekstart,'YYYY-MM') = '" + month + "' order by id");
		}
		/*else {
			listTree = myService.findListBySql("select to_char(plandate,'MM') as text, '" + month + "' || '-' || to_char(plandate,'MM') as id, 1 as leaf  from tab_plan_month where to_char(plandate,'YYYY') = '" + month + "' group by to_char(plandate,'MM') order by id");
		}	*/
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
        
        super.getResponse().getWriter().print(json.toString());
	}
	
	/**
	 * 获得左侧树
	 */
	public String toMonthTree() {
		
		List<HashMap<String, Object>> l = myService.findListBySql("select id as monthid, to_char(plandate, 'YYYY-MM') as currentmonth from tab_plan_month where TO_CHAR(plandate, 'yyyy-MM') = TO_CHAR(sysdate, 'yyyy-MM')  ");
		
		currentmonth = l.get(0).get("currentmonth").toString();
		monthid = l.get(0).get("monthid").toString();
		
		return "TREE_SUCCESS";
	}
	public String getBz() {
		return bz;
	}

	public String getIsAdmin() {
		return isAdmin;
	}

}
